package user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.entity.User;
import com.example.utils.Result;
import com.example.utils.ResultCode;
import javax.annotation.Resource;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import user.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RefreshScope
public class UserController {
    @Resource
    UserService userService;


    @Resource
    PasswordEncoder passwordEncoder;

    ResultCode resultCode = new ResultCode();

    @PostMapping("/signup")
    public Result registerUser(@RequestBody User signUp) {
        Result res = new Result();
        System.out.println(signUp.getUserName());
        if (userService.existsByUsername(signUp.getUserName())) {
            res.setCode(ResultCode.EXISTERR);
            res.setMessage("错误：用户名已存在!");
            return res;
        }

        if (userService.existsByEmail(signUp.getEmail())) {
            res.setCode(ResultCode.EXISTERR);
            res.setMessage("错误：邮箱已注册!");
            return res;
        }
        try {
            signUp.setPassword(passwordEncoder.encode(signUp.getPassword()));
            userService.save(signUp);
            res.setMessage("用户注册成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(ResultCode.InfoAddERR));
        }
        return res;
    }

    //得到用户列表
    @GetMapping("/userList")
//    @PreAuthorize("hasAuthority('admin')")
    public Result getCarrierList(@RequestParam(required = false,name = "roleId") Long roleId){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<User> list = userService.getInfo(roleId);
            map.put("list",list);
            res.setMessage("返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    // 更新用户信息
    @PutMapping("/modUser")
//    @PreAuthorize("hasAnyAuthority('admin','account','carrier','driver')")
    public Result modUser(@RequestBody User u){
        Result res = new Result();
        try {
            u.setPassword(passwordEncoder.encode(u.getPassword()));
            int r = userService.updateUserInfo(u);
            if(r > 0){
                res.setMessage("信息更新成功");
            }else{
                res.setCode(ResultCode.InfoModERR);
                res.setMessage(resultCode.getMsg(ResultCode.InfoModERR));
            }
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.ERROR);
            res.setMessage(resultCode.getMsg(ResultCode.ERROR));
        }
        return res;
    }

    //注销用户
    @DeleteMapping("/delUser")
//    @PreAuthorize("hasAnyAuthority('admin','account','carrier','driver')")
    public Result delUser(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            userService.delUser(id);
            res.setMessage("注销成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoDelERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoDelERR));
        }
        return res;
    }

    @GetMapping("/selectById")  // 根据id获取用户
    @SentinelResource(value = "selectUser", blockHandler = "test")
    User selectById(@RequestParam(required = false,name = "id") Long id){
//        throw new RuntimeException();
        return userService.getDetail(id);
    }

    User test(Long id, BlockException e){
        return new User("momo","无名氏",null,null,null,null);
    }

    @PostMapping("/signupDriver")
    public User registerDriver(@RequestBody User signUp){
        return userService.addDriver(signUp);
    }

    @GetMapping("/getDriverList")
    List<User> selectDriver(@RequestParam(required = false,name = "i")int i,
                            @RequestParam(required = false,name = "id")Long id){
        return userService.selectDriver(i,id);
    }
}
